package com.Sun.Helper;

import java.util.Stack;

import com.Sun.Model.encryptedIndex;
import com.Sun.Model.indexTreeNode;
import com.Sun.Model.pathInfo;
import com.Sun.index.indexEncrypted;
import com.Sun.index.indexHelper;

public class deleteFileHelper {
	public static Stack<pathInfo>getPathInfos(indexTreeNode root,indexTreeNode deleteNode,indexEncrypted inSK)
	{
		Stack<pathInfo>pathStack=new Stack<pathInfo>();
		int n=indexHelper.wordDisCount;
		deleteNode.setSearchIndex(new double[n]);
		indexTreeNode currNode=deleteNode;
		indexTreeNode broNode=null;
		while(currNode!=root)
		{
			pathInfo nodeInfo=new pathInfo(n);
			if(currNode.getParent().getLeft()==currNode)
			{
				broNode=currNode.getParent().getRight();
				nodeInfo.setPath(0);//左孩子设置为0
				System.out.println("左孩子");
			}
			else
			{
				broNode=currNode.getParent().getLeft();
				nodeInfo.setPath(1);//右孩子设置为1
				System.out.println("右孩子");
			}
			double[]broSearchIndex=broNode.getSearchIndex();
			double[]currSearchIndex=currNode.getSearchIndex();
			double[]tempIndex=new double[n];
			for(int i=0;i<broSearchIndex.length;i++)
			{
				if(broSearchIndex[i]>currSearchIndex[i])
				{
					tempIndex[i]=broSearchIndex[i];
				}
				else {
					tempIndex[i]=currSearchIndex[i];
				}
			}
			//nodeInfo.getNode().setSearchIndex(tempIndex);
			nodeInfo.getNode().setEnIndex(inSK.IndexEncrypted(tempIndex));
			pathStack.push(nodeInfo);
			currNode=currNode.getParent();
		}
		return pathStack;
	}
	public static int updateTreeByPath(indexTreeNode root,Stack<pathInfo> stack)
	{
		int flag=-1;
		indexTreeNode currNode=root;
		pathInfo currPathInfo=new pathInfo();
		while(!stack.isEmpty())
		{
			currPathInfo=stack.pop();
		    currNode.setEnIndex(currPathInfo.getNode().getEnIndex());
			if(currPathInfo.getPath()==0)
			{
				currNode=currNode.getLeft();
			}
			else {
				currNode=currNode.getRight();
			}
		}
		if(currNode.getLeft()==null&&currNode.getRight()==null)
		{
			System.out.println("更新要删除的叶子"+currNode.getFID());
			currNode.setEnIndex(new encryptedIndex());
			currNode.setFID("");
			flag=1;
		}
		return flag;
	}

}
